<?php
namespace app\servercomment\logic;

use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-06-13 14:15:09
 * @Description: 用户评论
 */
class Comment extends BaseLogic
{
	protected $name = 'comment';
    protected $config = 'db_config_comment';
    protected $commentModel = 'app\servercomment\model\Comment';
    protected $searchWhere = ['obj_id'=>'eq','user_id'=>'eq'];

    /*某用户评论某文章、商品等  可多次评论*/
    public function addComment($data): array 
    {
    	$res = $this->model($this->commentModel)->save($data);
    	return auto_return($res);
    }

    /*管理员批量删除评论*/
    public function delBatchComment($data): array 
    {
    	is_numeric($data['id']) && $data['id'] = [$data['id']];
    	$_where['id'] = ['in',$data['id']];
    	$_where['status'] = 1;
    	$res = $this->db()->where($_where)->setField('status', -1);
    	return auto_return($res);
    }

    /*db获取某条评论的内容*/
    public function dbCommontInfo($data)
    {
    	$_where['id'] = $data['id'];
    	$_where['status'] = 1;
    	return $this->db()->where($_where)->find();
    }

    /*顶某个评论*/
    public function topComment($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['status'] = 1;
        $res = $this->db()->where($_where)->setInc('top');
        return auto_return($res);
    }

    public function treadComment($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['status'] = 1;
        $res = $this->db()->where($_where)->setInc('tread');
        return auto_return($res);
    }

    public function comments($data = []): array 
    {
        $_where = $this->searchWhere($data,$this->searchWhere);
        $_where['status'] = 1;

        $comments = $this->db()
                    ->where($_where)
                    ->order('create_time', 'desc')
                    ->paginate(null,false,['page'=>$data['page'],'list_rows'=>$data['listRows']])
                    ->toArray();

        if($comments['total'] == 0){
            return auto_failure(null, '未查找到相应的记录');
        }

        $map['comment_id'] = get_field_to_array($comments['data']);
        $reply = logic('app\servercomment\logic\CommentReply')->dbReplys($map);

        foreach ($comments['data'] as $k => &$v) {
            if(isset($reply[$v['id']]) && $reply[$v['id']]){
                $v['allReplys'] = $reply[$v['id']];
            }else{
                $v['allReplys'] = [];
            }
        }

        return auto_return($comments);

    }
 



}